/*
 * OpenGL tools - phong shading example.
 * 
 * MIT License
 * 
 * Copyright (c) 2009, Markus Broecker <mbrckr@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <Shader.h>
#include <OpenGL.h>

#include <cstdlib>

// all opengl-tools are in the glTools namespace
using namespace glTools;

static int gWindow = 0;
static Shader* gShader = 0;

float gRotation = 0.f;
unsigned int gTimestamp = 0;

static void display()
{
	// clear the framebuffer
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	// setup the modelview matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
	
	// bind the shader and set some uniform variables
	gShader->bind();
	gShader->setUniform("light.color", 0.7, 0.7, 0.7);
	gShader->setUniform("light.direction", 2.0, 4.0, 0.2);
	
	// rotate the teapot
	glRotatef(gRotation, 0.f, 1.f, 0.f);
	
	// we have to flip surface direction for the teapot ...
	glFrontFace(GL_CW);
	glColor3f(0.7, 0.3, 0.1);
	glutSolidTeapot( 2.4 );
	glFrontFace(GL_CCW);
	
	// return to fixed function pipeline
	gShader->disable();
	
	// it's always a good idea to check for errors at least once per frame
	glError();
	
	// double buffering
	glutSwapBuffers();
}

static void keyboard(unsigned char key, int x, int y)
{
	// exit on escape
	if (key == 27)
	{
		delete gShader;
		exit(0);
	}
}

static void idle()
{
	// get the time to compute the delta - fractions of a second that passed
	unsigned int time = glutGet(GLUT_ELAPSED_TIME);
	float delta = static_cast<float>(time - gTimestamp) / 1000.f;
	gTimestamp = time;
	
	// update rotation
	gRotation += delta * 10.f;
	
	// request a new frame
	glutPostRedisplay();
}

int main(int argc, const char** argv)
{
	// init glut and set the parameters
	glutInit( &argc, const_cast<char**>(argv));
	glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
	glutInitWindowSize(512, 512);
	
	// create the window 
	gWindow = glutCreateWindow("shader example");
	
	#ifndef __APPLE__
		glewInit();
	#endif
	
	// register the callback functions
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutIdleFunc(idle);
	
	// setup opengl
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.f, 0.1f, 0.3f, 1.f);	
	
	// setup the projection matrix
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, 1.0, 1.0, 100.0);
	
	// load and compile a new shader from the source files
	gShader = new Shader("phong.vert", "phong.frag");
	
	// enter the main loop
	glutMainLoop();
	return 0;
}